package com.example.nacos.provider.controller;

import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.Timer;
import com.example.nacos.provider.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @ClassName : UserController
 * @Description : 提供用户服务api
 * @Author : tianshuo
 * @Date: 2021-03-17 09:54
 */
@RefreshScope
@RestController
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    static Map<Integer, User> userMap = new HashMap<>();

    @Value("${name}")
    String name;

    @Value("${age}")
    int age;

    @Value("${addr}")
    String addr;


    static {
        userMap.put(1, new User(1, "tianshuo"));
        userMap.put(2, new User(2, "lili"));
        userMap.put(3, new User(3, "cc"));
    }

    @GetMapping("/user/{id}")
    public User user(@PathVariable("id") Integer id) {
        logger.info("调用：{}", id);
        return new User(1, age, name, addr);
    }



    @Autowired
    private Meter requestMeter;

    @Autowired
    private Histogram responseSizes;

    @Autowired
    private Counter pendingJobs;

    @Autowired
    private Timer responses;




    @GetMapping("/user/list")
    public List<User> list() {
        User user = new User(1,"tianshuo","田营");
        User user1 = new User(2,"tianyulin","田营");
        User user2 = new User(3,"tianyuhao","田营");


        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user);
      //metrics配置 用来统计接口tps
     //   requestMeter.mark();

//        pendingJobs.inc();

//        responseSizes.update(new Random().nextInt(10));


//        final Timer.Context context = responses.time();
        try {
            return list;
        } finally {
//            context.stop();
        }
    }
}

